package com.flight.trans.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.flight.trans.R;
import com.flight.trans.adapter.LeavePortGroupBoardDetailsOperationAdapter;
import com.flight.trans.adapter.LeavePortGroupBoardDetailsResultAdapter;
import com.flight.trans.adapter.LeavePortGroupBoardDetailsSeeAdapter;
import com.flight.trans.bean.ContainerBean.ContainerData;
import com.flight.trans.bean.LeavePortGroupBoardBean.GroupBoardData;
import com.flight.trans.bean.LeavePortGroupBoardDetailBean.GroupBoardDetailData;
import com.flight.trans.bean.LeavePortGroupBoardDetailBean.GroupBoardDetailData.DetailsOrderData;
import com.flight.trans.bean.TransportSortBean;
import com.flight.trans.global.Constant;
import com.flight.trans.global.ConstantInfo;
import com.flight.trans.http.NetworkUtils;
import com.flight.trans.http.network.OkGoBackListener;
import com.flight.trans.listener.OnItemClickListener;
import com.flight.trans.listener.OnTextClickListener;
import com.flight.trans.util.CommonUtils;
import com.flight.trans.util.JsonUtils;
import com.flight.trans.util.LogUtils;
import com.flight.trans.view.dialog.AlertDialog;
import com.flight.trans.view.dialog.BoardSortDialog;
import com.flight.trans.view.dialog.ContainerAllDialog;
import com.flight.trans.view.edit.EditorActivity;
import com.flight.trans.view.edit.RemoveBoardEditorActivity;
import com.flight.trans.base.BaseActivity;
import com.flight.trans.util.ToastUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 出港组板
 */
public class LeavePortGroupBoardDetailsActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout mLlContainer ;
    private LinearLayout mLlTitleContainer,//标题
            mLlSeeContainer ;//查看
    private TextView mTvTitleName,//标题
            mTvFlightNo ,//航班号
            mTvETD ,//预起时间
            mTvDeep ;//目的站

    private TextView mTvSeeCount ,//查看数量
                mTvSeeWeight,//查看重量
                mTvOperationCount,//操作数量
                mTvOperationWeight, //操作重量
                mTvVolume ;//体积

    private RecyclerView mRvSeeContainer ,//查看
            mRvOperationContainer ,//操作
            mRvResultContainer ;//结果

    private ImageButton mIvSee ;

    private Button mBtnSubmit,//提交
            mBtnRevise,//修改
            mBtnSave;//保存

    private LinearLayout mLlGroupButton ;// 组板按钮

    private LeavePortGroupBoardDetailsSeeAdapter mSeeAdapter; // 查看
    private LeavePortGroupBoardDetailsOperationAdapter mOperationAdapter ;//操作
    private LeavePortGroupBoardDetailsResultAdapter mResultAdapter;

    // 获取到的数据
    private GroupBoardDetailData mGroupBoardDetailData ;

    private List<TransportSortBean> mSortList ;
    private List<DetailsOrderData> mGroupBoardDataList;

    private List<DetailsOrderData> mOrderResultDataList;

    private GroupBoardData mGroupBoardData ;
    private int OperationStatus ;

    @Override
    protected int setContentView() {
        return R.layout.activity_leave_port_group_board_details;
    }

    @Override
    protected void initView() {
        mLlContainer = findViewById(R.id.mLlContainer);
        mLlTitleContainer = findViewById(R.id.mLlTitleContainer);
        mTvTitleName = findViewById(R.id.mTvTitleName);
        mTvFlightNo = findViewById(R.id.mTvFlightNo);//航班号
        mTvDeep = findViewById(R.id.mTvDeep);//目的站
        mTvETD = findViewById(R.id.mTvETD);

        mRvSeeContainer = findViewById(R.id.mRvSeeContainer);//查看
        mRvOperationContainer = findViewById(R.id.mRvOperationContainer); //操作
        mRvResultContainer = findViewById(R.id.mRvResultContainer); //结果

        mTvSeeCount = findViewById(R.id.mTvSeeCount);
        mTvSeeWeight = findViewById(R.id.mTvSeeWeight);
        mTvOperationCount = findViewById(R.id.mTvOperationCount);
        mTvOperationWeight = findViewById(R.id.mTvOperationWeight);
        mTvVolume = findViewById(R.id.mTvVolume);

        mLlGroupButton = findViewById(R.id.mLlGroupButton);
        mLlSeeContainer = findViewById(R.id.mLlSeeContainer);

        mIvSee = findViewById(R.id.mIvSee);

        mBtnSubmit = findViewById(R.id.mBtnSubmit);
        mBtnRevise = findViewById(R.id.mBtnRevise);
        mBtnSave = findViewById(R.id.mBtnSave);
    }

    @Override
    protected void initData() {
        mGroupBoardData = (GroupBoardData) mBundle.getSerializable(Constant.INFO);
        OperationStatus = mBundle.getInt(Constant.OperationStatus);
        IsEditor = mBundle.getBoolean(Constant.IsEditor);

        CommonUtils.setScreenContainerPaddingTop(mLlContainer);
        CommonUtils.setScreenViewVisible(mLlTitleContainer);
        CommonUtils.setScreenViewVisible(mTvSeeCount);
        CommonUtils.setScreenViewVisible(mTvSeeWeight);
        CommonUtils.setScreenViewVisible(mTvOperationCount);
        CommonUtils.setScreenViewVisible(mTvOperationWeight);

        mTvFlightNo.setText(mGroupBoardData.getFlightNo());//航班号
        mTvDeep.setText(mGroupBoardData.getDeap());//目的站
        mTvETD.setText(CommonUtils.getTimeNorm(mGroupBoardData.getEtd()));

        mTvTitleName.setText(getString(R.string.title_leave_port_group_board));
        CommonUtils.onFinishTouch(mActivity , mLlContainer);
    }

    @Override
    protected void initConnect() {
        valueMap = new HashMap<>();
        valueMap.put(Constant.FlightID , mGroupBoardData.getFlightId());
        valueMap.put(Constant.ShippingBillID , mGroupBoardData.getShippingBillId());
        valueMap.put(Constant.Deap , mGroupBoardData.getDeap());

        NetworkUtils.getLeavePortGroupBoardDetails(new OkGoBackListener(mActivity) {
            @Override
            public void onSuccess(Object body) {
                // 整体数据
                mGroupBoardDetailData = (GroupBoardDetailData) body;
                mGroupBoardDetailData.getAssembleListData().setDispenseDate(mGroupBoardData.getDispenseDate());

                mGroupBoardDataList = mGroupBoardDetailData.getDetailDataList();

                mSortList = mGroupBoardDetailData.getTransportSort();

                initLocationData();
            }
        }, valueMap);
    }

    /**
     * 初始化按钮
     */
    private void initLocationData() {
        mBtnSubmit.setVisibility(IsEditor ? View.VISIBLE : View.GONE);//提交
        mBtnSave.setVisibility(IsEditor ? View.VISIBLE : View.GONE);//提交
        mLlGroupButton.setVisibility(IsEditor ? View.VISIBLE : View.GONE);//拆板
        mBtnRevise.setVisibility(IsEditor ||  OperationStatus == 2 ? View.GONE : View.VISIBLE);//修改

        // 初始化舱单内容
        initSeeAdapter();
        // 初始化操作区
        initOperationAdapter();
        // 初始化接过去
        initResultAdapter();
    }

    /**
     * 出库单
     */
    private void initSeeAdapter() {
        if(mSeeAdapter == null) {
            List<DetailsOrderData> detailsOrderDataList = initOperationList(mGroupBoardDetailData.getDetailDataList());

            mSeeAdapter = new LeavePortGroupBoardDetailsSeeAdapter(mActivity, detailsOrderDataList , detailsOrderDataList.size());
            mRvSeeContainer.setAdapter(mSeeAdapter);
            mRvSeeContainer.setLayoutManager(new LinearLayoutManager(mActivity));
        }else {
            mSeeAdapter.updateAdapter(mGroupBoardDetailData.getDetailDataList());
        }
    }

    /**
     * 结果，将同一个板的运单组合
     */
    private void initResultAdapter(){
        mOrderResultDataList = initResultBoardList();

        double volume = 0 ;
        for (int i = 0; i < mOrderResultDataList.size(); i++)
            volume += mOrderResultDataList.get(i).getVolumeSum();

        mTvVolume.setText("体积 / " + CommonUtils.getDouble(volume) + " m³");

        if(mResultAdapter == null) {
            mResultAdapter = new LeavePortGroupBoardDetailsResultAdapter(mActivity, mOrderResultDataList);
            mRvResultContainer.setAdapter(mResultAdapter);
            mRvResultContainer.setLayoutManager(new LinearLayoutManager(mActivity));

            mResultAdapter.setOnTextClickListener(new ResultOnTextClickListener());
        }else {
            mResultAdapter.updateAdapter(mOrderResultDataList);
        }
    }

    private class ResultOnTextClickListener implements OnTextClickListener {
        @Override
        public void onClick(View view, int position) {
            if(!IsEditor || OperationStatus == 2)
                return;

            EditorActivity.openActivity(mActivity, new EditorActivity.EditorCallbackListener() {
                @Override
                public void onSubmit(String content) {

                    double volume = TextUtils.isEmpty(content) ? 0 : Double.parseDouble(content);

                    boolean isContains = true ;

                    if(mSortList != null && mSortList.size() != 0){
                        for (int i = 0; i < mSortList.size(); i++) {
                            if(mSortList.get(i).getTransportCode().equals(initResultBoardList().get(position).getTransportCode())) {
                                mSortList.get(i).setVolume(volume);
                                isContains = false ;
                            }
                        }
                    }

                    if(isContains)
                        mSortList.add(new TransportSortBean(initResultBoardList().get(position).getTransportCode(), volume));

                    initResultAdapter();
                }
            } , CommonUtils.getDoubleFour(initResultBoardList().get(position).getVolumeSum()));
        }
    }

    /**
     * 获取拖斗结果
     */
    private List<DetailsOrderData> initResultBoardList(){
        Map<String , DetailsOrderData> groupBoardBeanMap = new HashMap<>();

        List<String> groupList = new ArrayList<>();

        // 拖斗归类
        for (int i = 0; i < mGroupBoardDataList.size(); i++) {
            DetailsOrderData groupBoardBean = mGroupBoardDataList.get(i);
            if (mSortList != null && mSortList.size() != 0) {
                for (int j = 0; j < mSortList.size(); j++) {
                    if (mSortList.get(j).getTransportCode().equals(groupBoardBean.getTransportCode())
                            && !groupList.contains(groupBoardBean.getTransportCode())) {
                        groupBoardBean.setVolumeSum(mSortList.get(j).getVolume());
                        groupList.add(groupBoardBean.getTransportCode());
                    }
                }
            }

            if(groupBoardBeanMap.containsKey(groupBoardBean.getTransportCode())){
                // 已经存在
                DetailsOrderData orderData = groupBoardBeanMap.get(groupBoardBean.getTransportCode());

                DetailsOrderData boardBean = new DetailsOrderData();
                boardBean.setTransportCode(orderData.getTransportCode());
                boardBean.setTransportType(orderData.getTransportType());
                boardBean.setOrderNo(orderData.getOrderNo() + "," + groupBoardBean.getOrderNo());
                boardBean.setNumber(orderData.getNumber() + groupBoardBean.getNumber());
                boardBean.setWeight(orderData.getWeight() + groupBoardBean.getWeight());
                boardBean.setVolume(orderData.getVolume() + groupBoardBean.getVolume());
                boardBean.setVolumeSum(orderData.getVolumeSum() + groupBoardBean.getVolumeSum());
                boardBean.setDeap(orderData.getDeap().contains(groupBoardBean.getDeap()) ? orderData.getDeap() : (orderData.getDeap() + "," + groupBoardBean.getDeap()));

                groupBoardBeanMap.put(boardBean.getTransportCode() , boardBean);
            }else {
                groupBoardBeanMap.put(groupBoardBean.getTransportCode() , groupBoardBean);
            }
        }

        // 转换归类
        List<DetailsOrderData> groupBoardBeans = new ArrayList<>();

        for(String key : groupBoardBeanMap.keySet()) {
            groupBoardBeans.add(groupBoardBeanMap.get(key));
        }

        return listSorting(groupBoardBeans) ;
    }

    /**
     * 操作
     */
    private void initOperationAdapter() {
        mGroupBoardDataList = initOperationList(mGroupBoardDataList);
        if(mOperationAdapter == null) {
            mOperationAdapter = new LeavePortGroupBoardDetailsOperationAdapter(mActivity, mGroupBoardDataList , IsEditor);
            mRvOperationContainer.setAdapter(mOperationAdapter);
            mRvOperationContainer.setLayoutManager(new LinearLayoutManager(mActivity));

            mOperationAdapter.setOnTextClickListener(new OperationOnTextClickListener());
            mOperationAdapter.setOnItemClickListener(new OperationOnItemClickListener());
        }else {
            mOperationAdapter.updateAdapter(mGroupBoardDataList , IsEditor);
        }
    }

    /**
     * 操作条目点击事件
     */
    private class OperationOnItemClickListener extends OnItemClickListener {
        @Override
        public void onClick(View view, int position) {
            if(mGroupBoardDataList.get(position).isSelected()){
                ToastUtils.showToast(R.string.toast_selected);
                return;
            }

            mGroupBoardDataList.get(position).setSelect(!mGroupBoardDataList.get(position).isSelect());
            mOperationAdapter.updateAdapter(mGroupBoardDataList);
        }

        @Override
        public void onLongClick(View view, int position) {
            mGroupBoardDataList.get(position).setSelected(!mGroupBoardDataList.get(position).isSelected());
            mGroupBoardDataList.get(position).setSelect(false);
            initOperationAdapter();
        }
    }

    /**
     * 操作条目斗号点击事件
     */
    private class OperationOnTextClickListener implements OnTextClickListener{
        @Override
        public void onClick(View view, int position) {
            if(mGroupBoardDataList.get(position).isSelected()){
                ToastUtils.showToast(R.string.toast_selected);
                return;
            }

            ContainerAllDialog.getInstance(mActivity)
                    .setOnItemClickListener(new ContainerAllDialog.OnSheetItemClickListener(){
                        @Override
                        public void onClick(ContainerData containerData) {
                            changeContainer(position , containerData);
                        }
                    })
                    .show();
        }
    }

    /**
     * 获取拖斗结果
     */
    private List<DetailsOrderData> initOperationList(List<DetailsOrderData> detailsOrderDataList){
        Map<String , DetailsOrderData> groupBoardBeanMap = new HashMap<>();

        // 拖斗归类
        for (int i = 0; i < detailsOrderDataList.size(); i++) {
            DetailsOrderData groupBoardBean = detailsOrderDataList.get(i);
            if(groupBoardBeanMap.containsKey(groupBoardBean.getTransportCode() + groupBoardBean.getOrderNo())){
                // 已经存在
                DetailsOrderData orderData = groupBoardBeanMap.get(groupBoardBean.getTransportCode() + groupBoardBean.getOrderNo());

                DetailsOrderData beanNew = new DetailsOrderData();
                if(orderData.getId() != 0)
                    beanNew.setId(orderData.getId());
                if(orderData.getOldId() != 0)
                    beanNew.setOldId(orderData.getOldId());
                beanNew.setOrderNo(orderData.getOrderNo());
                beanNew.setBusinessId(orderData.getBusinessId());
                beanNew.setCategory(orderData.getCategory());
                beanNew.setChannelId(orderData.getChannelId());
                beanNew.setStatus(orderData.getStatus());
                beanNew.setWarehouseAreaId(orderData.getWarehouseAreaId());
                beanNew.setWarehouseAreaName(orderData.getWarehouseAreaName());
                beanNew.setTransportCode(orderData.getTransportCode());
                beanNew.setTransportType(orderData.getTransportType());
                beanNew.setNumber(orderData.getNumber() + groupBoardBean.getNumber());
                beanNew.setWeight(orderData.getWeight() + groupBoardBean.getWeight());
                beanNew.setVolume(orderData.getVolume() + groupBoardBean.getVolume());
                beanNew.setDeap(orderData.getDeap().contains(groupBoardBean.getDeap()) ? orderData.getDeap() : (orderData.getDeap() + "," + groupBoardBean.getDeap()));

                groupBoardBeanMap.put(orderData.getTransportCode() + orderData.getOrderNo() , beanNew);
            }else {
                groupBoardBeanMap.put(groupBoardBean.getTransportCode() + groupBoardBean.getOrderNo() , groupBoardBean);
            }
        }

        // 转换归类
        List<DetailsOrderData> groupBoardBeans = new ArrayList<>();
        List<DetailsOrderData> groupBoardSelectedBeans = new ArrayList<>();

        for(String key : groupBoardBeanMap.keySet()) {
            if(groupBoardBeanMap.get(key).isSelected()){
                groupBoardSelectedBeans.add(groupBoardBeanMap.get(key));
            }else {
                groupBoardBeans.add(groupBoardBeanMap.get(key));
            }
        }

        Comparator<DetailsOrderData> comparator = new Comparator<DetailsOrderData>() {
            @Override
            public int compare(DetailsOrderData groupBoardStart, DetailsOrderData groupBoardEnd) {
                if (groupBoardStart.getNumber() < groupBoardEnd.getNumber())
                    return 1;
                else {
                    return -1;
                }
            }
        };

        //这里就会自动根据规则进行排序
        Collections.sort(groupBoardBeans, comparator);
        Collections.sort(groupBoardSelectedBeans, comparator);

        groupBoardBeans.addAll(groupBoardSelectedBeans);

        return groupBoardBeans ;
    }

    @Override
    public void onClick(View v) {
        if(CommonUtils.isFastDoubleClick())
            return;

        switch (v.getId()){
            case R.id.mIbBack:
                // 返回
                onBackPressed();
                break;
            case R.id.mIvSee:
                // 打开/收回
                if(mGroupBoardDetailData == null){
                    ToastUtils.showToast(R.string.toast_storeroom_inventory_data_null);
                    return;
                }

                mLlSeeContainer.setVisibility(mLlSeeContainer.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
                mIvSee.setImageResource(mLlSeeContainer.getVisibility() == View.GONE ? R.mipmap.icon_left_open : R.mipmap.icon_right_close);
                break;
            case R.id.mBtnSubmit :
                // 提交
                if(mGroupBoardDetailData == null){
                    ToastUtils.showToast(R.string.toast_storeroom_inventory_data_null);
                    return;
                }

                if(IsSaveIng){
                    ToastUtils.showToast(R.string.toast_save_ing);
                    return;
                }

                mOrderResultDataList = initResultBoardList();

                for (int i = 0; i < mOrderResultDataList.size(); i++) {
                    if(mOrderResultDataList.get(i).getVolumeSum() <= 0){
                        ToastUtils.showToast(R.string.toast_container_volume_null);
                        return;
                    }
                }

                AlertDialog.getInstance(mActivity)
                        .setContent(R.string.dialog_confirm_submit_group_board_info)
                        .setConfirmButton(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                clickSubmit(true);
                            }
                        }).show();
                break;
            case R.id.mBtnSave:
                // 保存
                if(mGroupBoardDetailData == null){
                    ToastUtils.showToast(R.string.toast_storeroom_inventory_data_null);
                    return;
                }

                if(IsSaveIng){
                    ToastUtils.showToast(R.string.toast_save_ing);
                    return;
                }

                AlertDialog.getInstance(mActivity)
                        .setContent(R.string.dialog_confirm_save_group_board_info)
                        .setConfirmButton(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                clickSubmit(false);
                            }
                        }).show();
                break;
            case R.id.mBtnRevise:
                // 修改
                if(mGroupBoardDetailData == null){
                    ToastUtils.showToast(R.string.toast_storeroom_inventory_data_null);
                    return;
                }

                IsEditor = true;
                initLocationData();
                break;
            case R.id.mBtnRemove:
                // 拆板
                if(mGroupBoardDetailData == null){
                    ToastUtils.showToast(R.string.toast_storeroom_inventory_data_null);
                    return;
                }

                if(getSelectedBoard().size() == 0){
                    ToastUtils.showToast(R.string.toast_please_select_order);
                }else if(getSelectedBoard().size() > 1){
                    ToastUtils.showToast(R.string.toast_select_order_one);
                }else {
                    ContainerAllDialog.getInstance(mActivity)
                            .setOnItemClickListener(new BoardOnItemClickListener(0)).show();
                }
                break;
            case R.id.mBtnMerge:
                // 合板
                if(mGroupBoardDetailData == null){
                    ToastUtils.showToast(R.string.toast_storeroom_inventory_data_null);
                    return;
                }

                if(getSelectedBoard().size() == 0){
                    ToastUtils.showToast(R.string.toast_please_select_order);
                }else if(getSelectedBoard().size() == 1){
                    ToastUtils.showToast(R.string.toast_select_order_no_one);
                }else {
                    ContainerAllDialog.getInstance(mActivity)
                            .setOnItemClickListener(new BoardOnItemClickListener(1)).show();
                }
                break;
            case R.id.mBtnSort:
                // 排序
                if(mGroupBoardDetailData == null){
                    ToastUtils.showToast(R.string.toast_storeroom_inventory_data_null);
                    return;
                }

                BoardSortDialog.getInstance(mActivity)
                        .setGroupBoardList(initResultBoardList())
                        .setConfirmButton(new BoardSortDialog.OnConfirmClickListener() {
                            @Override
                            public void onClick(List<TransportSortBean> sortList) {
                                mSortList = sortList ;

                                initOperationAdapter();
                                initResultAdapter();
                            }
                        })
                        .show();
                break;
        }
    }

    /**
     * 提交数据
     */
    private void clickSubmit(boolean isSave) {
        List<DetailsOrderData> resultBoardList = initResultBoardList();

        if(mSortList == null)
            mSortList = new ArrayList<>();

        // 补充遗漏
        for (int i = 0; i < resultBoardList.size(); i++) {
            boolean isContains = false ;

            for (int j = 0; j < mSortList.size(); j++) {
                if(mSortList.get(j).getTransportCode().equals(resultBoardList.get(i).getTransportCode()))
                    isContains = true;
            }

            if(!isContains) {
                mSortList.add(new TransportSortBean(resultBoardList.get(i).getTransportCode(), resultBoardList.get(i).getVolumeSum()));
            }
        }

        List<TransportSortBean> mList = new ArrayList<>();

        // 去除多余
        for (int i = 0; i < mSortList.size(); i++) {
            boolean isContains = false ;
            for (int j = 0; j < resultBoardList.size(); j++) {
                if(mSortList.get(i).getTransportCode().equals(resultBoardList.get(j).getTransportCode())){
                    isContains = true ;
                }
            }

            if(isContains)
                mList.add(mSortList.get(i));
        }

        mSortList = mList ;

        mGroupBoardDetailData.setDetailDataList(mGroupBoardDataList);
        mGroupBoardDetailData.setTransportSort(mSortList);
        mGroupBoardDetailData.setSave(isSave);

        IsSaveIng = true ;

        NetworkUtils.saveLeavePortGroupBoardDetails(new OkGoBackListener(mActivity) {
            @Override
            public void onSuccess(Object body) {
                CommonUtils.writeCrashLog("出港组板保存成功");
                IsSaveIng = false;
                ToastUtils.showToast(isSave ? R.string.toast_submit_success : R.string.toast_save_success);
                onFinish(Constant.RESULT_CODE);
            }

            @Override
            public void onError() {
                CommonUtils.writeCrashLog("出港组板保存失败");
                IsSaveIng = false;
            }
        }, JsonUtils.parseBeanToJson(mGroupBoardDetailData));
    }

    /**
     * 组板、拆板按钮
     */
    private class BoardOnItemClickListener extends ContainerAllDialog.OnSheetItemClickListener{
        private int type ;
        public BoardOnItemClickListener(int type){
            this.type = type ;
        }

        @Override
        public void onClick(ContainerData container) {
            if(type == 0){
                // 拆板
                mBundle = new Bundle();
                mBundle.putInt(Constant.Count , mGroupBoardDataList.get(getSelectedPosition()).getNumber());
                mBundle.putDouble(Constant.Weight , mGroupBoardDataList.get(getSelectedPosition()).getWeight());
                mBundle.putDouble(Constant.Volume , mGroupBoardDataList.get(getSelectedPosition()).getVolume());
                mBundle.putInt(Constant.Position , getSelectedPosition());
                RemoveBoardEditorActivity.openActivity(mActivity, new CallbackListener(container), mBundle);
            }else {
                // 合板
                for (int i = 0; i < mGroupBoardDataList.size(); i++) {
                    if(mGroupBoardDataList.get(i).isSelect()) {
                        mGroupBoardDataList.get(i).setTransportCode(container.getCode());
                    }
                    mGroupBoardDataList.get(i).setSelect(false);
                }

                List<TransportSortBean> list = new ArrayList<>();

                if(mSortList != null && mSortList.size() != 0) {
                    for (int i = 0; i < mSortList.size(); i++) {
                        if (!mSortList.get(i).getTransportCode().equals(container.getCode())) {
                            list.add(mSortList.get(i));
                        }
                    }
                }
                mSortList = list ;

                initOperationAdapter();
                initResultAdapter();
            }
        }
    }

    /**
     * 拆板监听
     */
    private class CallbackListener implements RemoveBoardEditorActivity.EditorCallbackListener {

        private ContainerData container ;

        public CallbackListener(ContainerData container){
            this.container = container ;
        }

        @Override
        public void onSubmit(Bundle bundle) {
            int count = bundle.getInt(Constant.Count) ;
            double widget = bundle.getDouble(Constant.Weight) ;
            double volume = bundle.getDouble(Constant.Volume) ;

            removeContainer(count , widget , volume , container);
        }
    };

    private void changeContainer(int position , ContainerData container){
        LogUtils.e("container" , container == null);

        mGroupBoardDataList.get(position).setTransportCode(container.getCode());
        mGroupBoardDataList.get(position).setTransportType(container.getType());
        mGroupBoardDataList.get(position).setOldId(mGroupBoardDataList.get(position).getId());
        mGroupBoardDataList.get(position).setId(0);

        initOperationAdapter();
        initResultAdapter();
    }

    private void removeContainer(int count , double widget , double volume , ContainerData container){
        DetailsOrderData orderData = mGroupBoardDataList.get(getSelectedPosition());

        List<TransportSortBean> list = new ArrayList<>();

        if(mSortList != null && mSortList.size() != 0) {
            for (int i = 0; i < mSortList.size(); i++) {
                if (!mSortList.get(i).getTransportCode().equals(mGroupBoardDataList.get(getSelectedPosition()).getTransportCode()) && mSortList.get(i).getTransportCode().equals(container.getCode())) {
                    list.add(mSortList.get(i));
                }
            }
        }
        mSortList = list ;

        if(count == orderData.getNumber() && widget == orderData.getWeight() && volume == orderData.getVolume()){
            mGroupBoardDataList.get(getSelectedPosition()).setOldId(mGroupBoardDataList.get(getSelectedPosition()).getId());
            mGroupBoardDataList.get(getSelectedPosition()).setId(0);
            mGroupBoardDataList.get(getSelectedPosition()).setTransportCode(container.getCode());
        }else {
            mGroupBoardDataList.get(getSelectedPosition()).setNumber(orderData.getNumber() - count);
            mGroupBoardDataList.get(getSelectedPosition()).setWeight(orderData.getWeight() - widget);
            mGroupBoardDataList.get(getSelectedPosition()).setVolume(orderData.getVolume() - volume);
            mGroupBoardDataList.get(getSelectedPosition()).setSelect(false);

            DetailsOrderData beanNew = new DetailsOrderData();
            beanNew.setOldId(orderData.getId());
            beanNew.setOrderNo(orderData.getOrderNo());
            beanNew.setBusinessId(orderData.getBusinessId());
            beanNew.setCategory(orderData.getCategory());
            beanNew.setChannelId(orderData.getChannelId());
            beanNew.setStatus(orderData.getStatus());
            beanNew.setWarehouseAreaId(orderData.getWarehouseAreaId());
            beanNew.setWarehouseAreaName(orderData.getWarehouseAreaName());
            beanNew.setTransportCode(container.getCode());
            beanNew.setTransportType(orderData.getTransportType());
            beanNew.setDeap(orderData.getDeap());
            beanNew.setNumber(count);
            beanNew.setWeight(widget);
            beanNew.setVolume(volume);
            beanNew.setPosition(Constant.SortNullPosition);
            beanNew.setSelect(true);

            mGroupBoardDataList.add(getSelectedPosition(), beanNew);
        }
        initOperationAdapter();
        initResultAdapter();
    }

    /**
     * 获取已选择的运单
     * @return
     */
    public List<DetailsOrderData> getSelectedBoard(){
        List<DetailsOrderData> list = new ArrayList<>();
        for (int i = 0; i < mGroupBoardDataList.size(); i++) {
            if(mGroupBoardDataList.get(i).isSelect())
                list.add(mGroupBoardDataList.get(i));
        }

        return list ;
    }


    /**
     * 获取已选择的运单
     * @return
     */
    public int getSelectedPosition(){
        int position = 0 ;
        for (int i = 0; i < mGroupBoardDataList.size(); i++) {
            if(mGroupBoardDataList.get(i).isSelect())
                position = i ;
        }

        return position ;
    }

    /**
     * 集合排序
     * @param mList
     * @return
     */
    private List<DetailsOrderData> listSorting(List<DetailsOrderData> mList) {

        for (int i = 0; i < mList.size(); i++) {
            if (mSortList != null && mSortList.size() != 0) {
                int isContains = -1;
                for (int j = 0; j < mSortList.size(); j++) {
                    if (mSortList.get(j).getTransportCode().equals(mList.get(i).getTransportCode())) {
                        isContains = j;
                    }
                }

                if (isContains != -1) {
                    mList.get(i).setPosition(isContains + 1);
                } else {
                    mList.get(i).setPosition(Constant.SortNullPosition);
                }
            } else {
                mList.get(i).setPosition(Constant.SortNullPosition);
            }
        }

        Comparator<DetailsOrderData> comparator = new Comparator<DetailsOrderData>() {
            @Override
            public int compare(DetailsOrderData groupBoardStart, DetailsOrderData groupBoardEnd) {
                if (groupBoardStart.getPosition() > groupBoardEnd.getPosition())
                    return 1;
                else {
                    return -1;
                }
            }
        };

        //这里就会自动根据规则进行排序
        Collections.sort(mList, comparator);

        return mList ;
    }

    @Override
    protected void initRefresh(int count) {
        super.initRefresh(count);

        NetworkUtils.checkPageLock(new OkGoBackListener(mActivity) , ConstantInfo.OUT_STORAGE_ASSEMBLE , mGroupBoardData.getFlightId());
    }
}
